package com.xbts.system.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xbts.system.domain.CnOrderItem;
import com.xbts.system.domain.CnUserCloudServersRelation;
import com.xbts.system.domain.dto.CnOrderItemReviewDTO;
import com.xbts.system.domain.vo.CnOrderItemReviewVO;
import com.xbts.system.domain.vo.CnOrderStatisticsVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 子订单Mapper接口
 * 
 * @author xbts
 * @date 2023-08-03
 */
public interface CnOrderItemMapper extends BaseMapper<CnOrderItem> {

    /**
     * 查询子订单列表
     *
     * @param queryWrapper 子订单
     * @return 子订单集合
     */
    @Select("select \n" +
            "(select SUM(update_price) from cn_order as coi left join sys_user su on coi.user_id = su.user_id ${ew.customSqlSegment}) totalAmount, \n" +
            "(select count(*) from cn_order as coi  left join sys_user su on coi.user_id = su.user_id ${ew.customSqlSegment}) transactionOrderNum,\n" +
            "(select ROUND(avg(update_price),2) from cn_order as coi left join sys_user su on coi.user_id = su.user_id ${ew.customSqlSegment}) avgAmount ")
    CnOrderStatisticsVO statisticsList(@Param(Constants.WRAPPER) QueryWrapper<CnOrderItem> queryWrapper);

    List<CnOrderItemReviewVO> reviewList(CnOrderItemReviewDTO cnOrderItemReviewDTO);

    @Select("select order_item_id,coi.order_id,coi.user_id,coi.region_id,su.nick_name as create_by,coi.create_time,coi.update_by,\n" +
            "coi.update_time,coi.remark,coi.json_str,coi.update_price,coi.original_price,coi.discount_price,coi.shopping_car_enums,\n" +
            "coi.order_abstract,coi.discount,coi.relation_id,coi.auto_renew,coi.period_num,coi.period_type,coi.shopping_car_type,\n" +
            "coi.cn_user_cloud_servers_relation_id,coi.invoicing,coi.market_sku_id,coi.contract_status,coi.order_status,coi.sn, co.payment_voucher_address\n" +
            "from cn_order_item coi \n" +
            "left join cn_order co on co.order_id = coi.order_id \n" +
            "left join sys_user su on coi.user_id = su.user_id ${ew.customSqlSegment}")
    List<CnOrderItem> queryList(@Param(Constants.WRAPPER) QueryWrapper<CnOrderItem> queryWrapper);

    @Select("select count(1) from cn_user_cloud_servers_relation cucsr left join sys_user su on cucsr.user_id = su.user_id ${ew.customSqlSegment} ")
    long getTransactionNum(@Param(Constants.WRAPPER) QueryWrapper<CnUserCloudServersRelation> transactionNumQueryWrapper);
}